package com.xbongbong.saas.help;

import com.xbongbong.parent.data.FormDataEsService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.detailtab.dto.DetailTabDataGetDTO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.form.pojo.dto.EsDataDTO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.enums.OutstockTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2021/08/04 10:20
 */
@Component
public class SaasOutstockHelp {


    @Resource
    private PaasAppModel paasAppModel;
    @Resource
    private FormDataEsService formDataEsService;

    public PaasFormDataEsListVO getOutstockListFromEs(String corpid, Long refId, Integer type, Integer distributorMark) throws XbbException{
        DetailTabDataGetDTO detailTabDataGetDTO = new DetailTabDataGetDTO();
        detailTabDataGetDTO.setCorpid(corpid);
        detailTabDataGetDTO.setDataId(refId);
        detailTabDataGetDTO.setPage(1);
        detailTabDataGetDTO.setPageSize(20);
        detailTabDataGetDTO.setDistributorMark(distributorMark);
        return getOutstockListFromEs(detailTabDataGetDTO, type,
                Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType(), FlowStatusEnum.APPROVAL_PENDING.getType(), FlowStatusEnum.IN_APPROVAL.getType()),null);

    }

    public PaasFormDataEsListVO getOutstockListFromEs(DetailTabDataGetDTO detailTabDataGetDTO, Integer type, List<Object> flowStatus, List<ConditionsEntityExt> conditionsEntityExts) throws XbbException {
        String corpid = detailTabDataGetDTO.getCorpid();
        Boolean isUseJxc = paasAppModel.validateAppEnable(null, XbbRefTypeEnum.JXC.getAlias(), SaasMarkEnum.SAAS.getCode(), corpid);
        if (!isUseJxc) {
            // 未开启进销存
            return new PaasFormDataEsListVO();
        }
        EsDataDTO esDataDTO = new EsDataDTO();
        esDataDTO.setDistributorMark(detailTabDataGetDTO.getDistributorMark());
        esDataDTO.setCorpid(corpid);
        esDataDTO.setBusinessType(OutstockTypeEnum.getByCode(type).getStockBusinessType());
        esDataDTO.setFieldList(null);
        esDataDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
        esDataDTO.setDel(0);
        esDataDTO.setPage(detailTabDataGetDTO.getPage());
        esDataDTO.setPageSize(detailTabDataGetDTO.getPageSize());
        // 条件拼装
        List<ConditionsEntityExt> conditionsEntityExtList = getConditionList(type, detailTabDataGetDTO.getDataId(), flowStatus, conditionsEntityExts);
        esDataDTO.setConditionsEntityExtList(conditionsEntityExtList);
        // 按addTime降序排列
        Map<String, String> sortMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        sortMap.put(BusinessConstant.ORDER, SortOrder.DESC.toString());
        sortMap.put(BusinessConstant.FIELD, ParameterConstant.ADD_TIME);
        esDataDTO.setSortMap(sortMap);

        PaasFormDataEsListVO esDataList = formDataEsService.getEsDataList(esDataDTO);
        return esDataList;
    }


    public static List<ConditionsEntityExt> getConditionList(Integer type, Long refId, List<Object> flowStatus, List<ConditionsEntityExt> conditionsEntityExts) {
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionsUtil.isNotEmpty(conditionsEntityExts)) {
            conditionsEntityExtList = conditionsEntityExts;
        }
        // Type，关联单据的类型
        ConditionsEntityExt typeConditionsEntityExt = new ConditionsEntityExt();
        typeConditionsEntityExt.setAttr(OutstockEnum.TYPE.getAttr());
        typeConditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        typeConditionsEntityExt.setValue(Arrays.asList(String.valueOf(type)));
        typeConditionsEntityExt.setFieldType(OutstockEnum.TYPE.getFieldType());
        conditionsEntityExtList.add(typeConditionsEntityExt);
        // refID 关联单据Id
        ConditionsEntityExt refIdConditionsEntityExt = new ConditionsEntityExt();
        refIdConditionsEntityExt.setAttr(OutstockEnum.REF_ID.getAttr());
        refIdConditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        refIdConditionsEntityExt.setValue(Arrays.asList(String.valueOf(refId)));
        refIdConditionsEntityExt.setFieldType(OutstockEnum.REF_ID.getFieldType());
        conditionsEntityExtList.add(refIdConditionsEntityExt);

        // flowStatus
        ConditionsEntityExt flowStatusConditionsEntityExt = new ConditionsEntityExt();
        flowStatusConditionsEntityExt.setAttr("flowStatus");
        flowStatusConditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
        flowStatusConditionsEntityExt.setValue(flowStatus);
        flowStatusConditionsEntityExt.setFieldType(FieldTypeEnum.DATAID.getType());
        conditionsEntityExtList.add(flowStatusConditionsEntityExt);
        return conditionsEntityExtList;
    }

}
